new Vue({
    el: '#app',
    data() {
        return {
            html:'',
            options: [{
                value: 0,
                label: '通知公告'
            }, {
                value: 1,
                label: '行情描述'
            }, {
                value: 2,
                label: '国内资讯'
            }, {
                value: 3,
                label: '国际资讯'
            }, {
                value: 4,
                label: '公司动态'
            }],
            form: {
                News_SerialNumber: "",
                News_Name: '',
                News_Type: 0,
                Enterprise_Info_SerialNumber: $.cookie("enterprise"),
                News_Date: '',
                News_Main:'',
                Picture_URL:'',
            }
        }
    },
    mounted() {
        this.getNews()
    },
    methods: {
        handlePictureCardPreview(file) {
            this.dialogVisible = true
            this.dialogImageUrl = file
        },
        handleRemove(type, index) {
            this.form.Picture_URL = ''
        },
        uploadFront(file) {
            let _this = this
            let num = 1
            if (file.size > 150000 && file.size < 500000) {
                num = 0.4
            } else if (file.size > 500000) {
                num = 0.2
            }
            new Compressor(file, {
                quality: num,
                // if(file.size) {
                // }
                success(file) {
                    console.log(file.size)
                    const formData = new FormData();
                    formData.append('file', file, file.name);
                    let oFileReader = new FileReader();
                    oFileReader.readAsDataURL(file);
                    oFileReader.onload = function (e) {
                        $.ajax({
                            type: 'POST',
                            url: DomainName + '/BusinessPIC',
                            data: {
                                Data: e.target.result,
                                DataLength: file.size,
                                FileName: file.name,
                                logo:true
                            },
                            complete: function (XMLHttpRequest) {
                                _this.loading = false
                                if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                                    window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                                }
                            },
                            success: function (Result) {
                                _this.form.pic = e.target.result
                                if (Result.Status == "success") {
                                    _this.form.Picture_URL = Result.Picture_Url
                                    _this.$message.success('上传成功')
                                    console.log(Result.Picture_Url);
                                } else {
                                    _this.$message.error('上传失败')
                                }
                            },
                            error: function () {
                                _this.$message.error("操作失败，请稍后再试")
                            },
                            dataType: "json"
                        });
                    }
                },
                error(err) {
                    console.log(err.message);
                },
            });
        },
        getNews(){
            const { createEditor, createToolbar } = window.wangEditor
            const this_ = this
            const editorConfig = {
                placeholder: '新闻内容',
                onChange(editor) {
                    this_.html = editor.getHtml()
                    console.log(this_.html)
                    // 也可以同步到 <textarea>
                }
            }
            const editor = createEditor({
                selector: '#editor-container',
                html: '<p><br></p>',
                config: editorConfig,
                mode: 'simple', // or 'simple'
            })
    
            const toolbarConfig = {}
    
            const toolbar = createToolbar({
                editor,
                selector: '#toolbar-container',
                config: toolbarConfig,
                mode: 'default', // or 'simple'
            })
        },
        back(){
            window.history.go(-1)
        },
        subMessage() {
            console.log(this.html)
            if(this.html==''){
                this.$message.error("请输入新闻标题")
                return
            }
            this.form.News_Main = this.html
            this.form.News_Date = getYMDHMSDate()
            const _this = this
            console.log(this.form)
            $.ajax({
                url: DomainName + '/GoodsFileAdd',
                type: 'POST',
                data: {
                  Data: JSON.stringify({
                    news_info: [_this.form]
                  }),
                  token: $.cookie("token")
                },
                success: function (res) {
                  console.log('提交结果', res)
                  if(res.Status=='success'){
                    _this.$message.success("发布成功!")
                    window.history.go(-1)
                  }
                },
                error: function () {
                  _this.$message.error("操作失败，请稍后再试")
                },
                complete: function (XMLHttpRequest) {
                  _this.loading = false
                  if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                    window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                  }
                }
              })
        }
    },
})